package day02;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class FileChannelDemo01 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        File file = new File("D:\\1\\fileChannel.txt");
        FileOutputStream fout = new FileOutputStream(file,true);
        FileChannel fileChannel = fout.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(true) {
            System.out.println("请输入数据:");
            String line = sc.nextLine();
            buffer.put(line.getBytes(StandardCharsets.UTF_8));
            if ("886".equals(line.trim())) {
                break;
            }
            //进行模式转换
            buffer.flip();
            fileChannel.write(buffer);
            buffer.clear();
        }
        fileChannel.close();
        fout.close();

    }
}
